package main

import (
	"sync"
	"time"
)

var (
	giftRoom     = map[int]int{}
	giftRoomLock sync.RWMutex
)

func giftRoomHave(roomid int) bool {
	giftRoomLock.RLock()
	defer giftRoomLock.RUnlock()
	_, ok := giftRoom[roomid]
	return ok
}

func giftRoomAdd(roomid, endTime int) {
	giftRoomLock.Lock()
	defer giftRoomLock.Unlock()
	giftRoom[roomid] = endTime
	go func() {
		t := endTime - int(nowTime()) + 1
		//fmt.Printf("\r %v秒后删除 %v \n", t, roomid)
		if t > 0 {
			time.Sleep(time.Duration(t) * time.Second)
		}
		giftRoomDel(roomid)
	}()
}

func giftRoomDel(roomid int) {
	giftRoomLock.Lock()
	defer giftRoomLock.Unlock()
	if end, ok := giftRoom[roomid]; ok {
		if nowTime() >= int64(end) {
			delete(giftRoom, roomid)
			//fmt.Println("删除", roomid)
		}
	}
}
